<?php

class ParameterSelectionOptionsContainerForm extends ProductForm
{
	public $options = array();
	public $parameter = null;
	public $langs;
	
  public function configure(){
  	
    if( !$this->getParameter() )
    	return;


	$this->setupNewOption();


    $this->widgetSchema['parameter_options_list'] = new sfWidgetFormDoctrineChoice(
    			array(	'multiple' => $this->isMultiple(),
    					'model' => 'ParameterOption',
    					'query' => $this->getQuery(),
    					'method' => 'getTitleForForm' ));

	

	
    			
    $this->validatorSchema['parameter_options_list'] = new sfValidatorPass();
    
    		
	
    	
   $this->useFields( array('parameter_options_list', 'id','new_option') );
    
  }
  
  
  public function getQuery(){
	return Doctrine_Query::create() 
			->from('ParameterOption po')
			->leftJoin('po.Translation t')
			->leftJoin('po.Parameter p')
			->where('po.param_id = ?', $this->getParameter()->getId())
			->setHydrationMode(Doctrine::HYDRATE_RECORD);
  }
  
  public function doSave( $con = null ){
  	
	$pk = $this->getQuery()->execute()->getPrimaryKeys();



	Doctrine_Query::create()
		->delete('ParameterProductOption ppo')
		->where('product_id = ?' , $this->getObject()->getId() )
		->whereIn('option_id', $pk )
		->execute();



	unset($this->embeddedForms['new_option']);
	$this->validatorSchema['new_option'] = new sfValidatorPass();
	
	if($this->getValue('new_option')){
	    $this->saveNewOption($this->getValue('new_option'));
	}


	if( is_array( $this->getValue('parameter_options_list') ) && $this->getValue('parameter_options_list')){
	    foreach( $this->getValue('parameter_options_list') as $paramOptionID ){
		    $ppo = new ParameterProductOption();
		    $ppo->fromArray( array( 'product_id' => $this->getObject()->id,
					    'option_id' => $paramOptionID ) );
		    $ppo->save();
	    }
	}else if($this->getValue('parameter_options_list')){
		    $ppo = new ParameterProductOption();
		    $ppo->fromArray( array( 'product_id' => $this->getObject()->id,
					    'option_id' => $this->getValue('parameter_options_list') ) );
		    $ppo->save();
	}
		
			
  }


  protected function saveNewOption($new_option){
	  $option = new ParameterOption();
	  
	if($this->getParameter()->getMultilang()){

		$found_translation = false;

		foreach($new_option as $lang => $option_trans){
			if(is_array($option_trans) && !empty($option_trans) && $option_trans['name']){
				$found_translation = true;
				$option->Translation[$lang]->fromArray($option_trans);
			}
		}

		if(!$found_translation) return;
		
		$option->Parameter = $this->getParameter();
		$option->save();

		$ppo = new ParameterProductOption();
		$ppo->option_id = $option->id;
		$ppo->product_id = $this->getObject()->getId();
		$ppo->save();


	}else{
		if(!$new_option['title']) return;
		$option = new ParameterOption();
		$option->fromArray($new_option);
		$option->Parameter = $this->getParameter();
		$option->save();

		$ppo = new ParameterProductOption();
		$ppo->option_id = $option->id;
		$ppo->product_id = $this->getObject()->getId();
		$ppo->save();
	}
	
  }

  protected function setupNewOption(){
		$this->embedForm('new_option', new ParameterOptionForm());
	  
  }
  
  
  
  public function isMultiple(){  	
  	return $this->getParameter()->getType() == 'MANY_OPTIONS';
  }
  
  
  public function getEditFormRendereringTemplate(){	
  	return "singlelang_selection";
  }
	
  
  
  public function getParameter(){
  	return $this->parameter;
  }
  
  public function setParameter( $parameter ){
  	$this->parameter = $parameter;
  }
  
  
  public function getLanguages(){
	  $languages = array();
  	foreach( Doctrine::getTable('Language')->findAll() as $lang ){
  		$languages[] = $lang->getAbr();
  	}

	return $languages;
  }
  
  
  public function setLanguages( array $langs ){

  	$this->langs = $langs;
  	
  }
  
  
}
